package com.dong.nettyconcurrent.c03_chat;


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MyChatClient {
    public static void main(String[] args) {
        NioEventLoopGroup group = new NioEventLoopGroup();
        try{
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new MyChatClientInitializer());
            ChannelFuture channelFuture = bootstrap.connect("localhost", 8899).sync();
          //  String  msg = null;
        /*    byte[] buff = new byte[1024];
            int len = 0;
            while(true) {
                len = System.in.read(buff);
                String msg = new String(buff);
                channelFuture.channel().writeAndFlush(msg);
            }*/
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            Channel channel = channelFuture.channel();
            for(;;) {
                channel.writeAndFlush(br.readLine()+"\r\n");
            }

            //        channelFuture.channel().closeFuture().sync();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();
        }
    }
}
